jsx3.Class.defineClass(
        "tibco.admin.monitormanage.uicomponents.table.timeComparison.serviceStats.ServiceStats", //the full name of the class to create
        jsx3.gui.Block, //  extends
        null, // implements
        function (plugin) {
        
        	plugin.serviceTimeComparisonCacheId = "SPM_SERVICE_TIMECOMPARISON_TABLE_CACHE";
        	plugin.serviceInstanceTimeComparisonCacheId = "SPM_SERVICE_INSTANCE_TIMECOMPARISON_TABLE_CACHE";
        
        	plugin.prototype.generalStatsInitialized = false;
        	plugin.prototype.sinceStartedStatsInitialized = false;
        	
        	plugin.prototype.isServiceInstance;
        
            plugin.prototype.init = function() {
                this.jsxsuper();
            }
            
            plugin.prototype.getNamespace = function() {
               return "tibco.admin";
            }
            
            plugin.prototype.setUpComponent = function(isServiceInstance) {
	            this.generalStatsInitialized = false;
    	    	this.sinceStartedStatsInitialized = false;
            	this.isServiceInstance = isServiceInstance;
            	
            	var subject = tibco.admin.monitormanage.uicomponents.misc.servicePerformance.ServicePerformance.getCacheInfoSubject(true, isServiceInstance);
            	tibco.admin.monitormanage.general.PageBusUtils.subscribe(subject, this.loadTableForSinceStarted, this);
            	subject = tibco.admin.monitormanage.uicomponents.misc.servicePerformance.ServicePerformance.getCacheInfoSubject(false, isServiceInstance);
            	tibco.admin.monitormanage.general.PageBusUtils.subscribe(subject, this.loadTableForGeneral, this);
            }
            
            plugin.prototype.loadTableForSinceStarted = function(subject, message, serviceStatsObj) {
       			serviceStatsObj.sinceStartedStatsInitialized = true;
       			if(serviceStatsObj.generalStatsInitialized)
       			{ serviceStatsObj.loadTable(serviceStatsObj); }
            }
            
            plugin.prototype.loadTableForGeneral = function(subject, message, serviceStatsObj) {
            	serviceStatsObj.generalStatsInitialized = true;
            	if(serviceStatsObj.sinceStartedStatsInitialized)
            	{ serviceStatsObj.loadTable(serviceStatsObj); }
            }
            
            plugin.prototype.loadTable = function(serviceStatsObj) {
            	serviceStatsObj.sinceStartedStatsInitialized = false;
            	serviceStatsObj.generalStatsInitialized = false;
            	
            	var server = tibco.admin.Main.getServer();
    	       	var sinceStartedCachedDoc = server.Cache.getDocument(tibco.admin.monitormanage.uicomponents.misc.servicePerformance.ServicePerformance.getCacheInfoId(true, serviceStatsObj.isServiceInstance));
    	       	var generalCachedDoc = server.Cache.getDocument(tibco.admin.monitormanage.uicomponents.misc.servicePerformance.ServicePerformance.getCacheInfoId(false, serviceStatsObj.isServiceInstance));
            
            	var sinceStartedRecord = sinceStartedCachedDoc.getFirstChild();
            	var generalRecord = generalCachedDoc.getFirstChild();
				
				var server = tibco.admin.Main.getServer();
            	
            	var tableDoc = jsx3.xml.CDF.Document.newDocument();
				
            	var recordAttrs = plugin.createRecordAttrs("throughputId", server.getDynamicProperty("@MM@general@svcStatsThroughputTitle"), generalRecord.getAttribute("throughput"), sinceStartedRecord.getAttribute("throughput"));
				
            	tableDoc.insertRecord(recordAttrs);
            	recordAttrs = plugin.createRecordAttrs("avgResponseTimeId", server.getDynamicProperty("@MM@general@svcStatsAveResponseTimeTitle"), generalRecord.getAttribute("avgResponseTime"), sinceStartedRecord.getAttribute("avgResponseTime"));
				
            	tableDoc.insertRecord(recordAttrs);
            	recordAttrs = plugin.createRecordAttrs("requestsId", server.getDynamicProperty("@MM@general@svcStatsTotalRequestsTitle"), generalRecord.getAttribute("hitCount"), sinceStartedRecord.getAttribute("hitCount"));
				
            	tableDoc.insertRecord(recordAttrs);
            	
            	var tableCacheId = plugin.serviceTimeComparisonCacheId;
      			if(serviceStatsObj.isServiceInstance)
      			{ tableCacheId = plugin.serviceInstanceTimeComparisonCacheId; }
      			tibco.admin.Main.getServer().Cache.setDocument(tableCacheId, tableDoc);
      			
      			var tableObj = serviceStatsObj.getDescendantOfName("serviceStatsTable");
      			tableObj.setXMLId(tableCacheId);
      			
      			// set the timeframe column header name
      			var timeframeCol = tableObj.getDescendantOfName("timeframe1Col");
      			var timeframeName = tibco.admin.monitormanage.uicomponents.dropdown.timeframe.Timeframe.getCurrentTimeframeName();
      			timeframeCol.setText(timeframeName, true);
      			tableObj.repaintData();
            }
            
            plugin.createRecordAttrs = function(jsxidVal, header, data1, data2) {
            	var recordAttrs = new Object();
            	recordAttrs.jsxid = jsxidVal;
            	recordAttrs.header = header;
            	recordAttrs.dataCol1 = data1;
            	recordAttrs.dataCol2 = data2;
            	return recordAttrs;
            }

        });
